// pages/category/category.ts
const apiCategory = require('../../api/request')
interface Item{
  category_id: ''
  shop_id: ''
}
interface Sitem{
  shop_id:''
  shop_name:''
  category_name:''
  shop_img:''
}
interface Citem{
  category_id: ''
}
interface arr{
  shop_id_arr: ''
}
interface name{

}
// interface 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrshop:[],
    currentTitle: ''
  },
  onLoad(options) {
    const category_id:any = options.category_id
    // console.log("页面：",category_id)
    this.categorygetFn(category_id)
  },
  async categorygetFn(category_id1: string){
    const content:any = await apiCategory.categoryget()
    // console.log("数据1：",content)
    if(content.code == 200){
      const shop_id_arr = content.content.map((item: Item)=>{
        return {
          category_id:item.category_id,
          shop_id_arr: item.shop_id.split(',')
        }
      })
      // console.log(shop_id_arr)
      const category_id_arr = shop_id_arr.filter((item: Citem)=>{
        return category_id1 == item.category_id
      })
      // console.log("ID:",category_id_arr)
      const shopIds = category_id_arr.flatMap((item: { shop_id_arr: any }) => item.shop_id_arr)
      // console.log("提取的商店ID:", shopIds);
      const shopData = await this.shopgetFn()
      const filterShops = shopData.filter((shop: Sitem) => {
        return shopIds.includes(String(shop.shop_id));
      });
      // console.log("匹配：",filterShops);
      const currentTitle = filterShops.map((item: { category_name: any }) => item.category_name)
      // console.log(currentTitle[0])
      this.setData({
        arrshop:filterShops,
        currentTitle: currentTitle[0]
      })
    }
    // let {code:numner,result:any} = content

  },
  async shopgetFn(){
    const content:any = await apiCategory.shopget()
    // console.log(content)
    if(content.code == 200){
      const shoparr = content.content.map((item:Sitem)=>{
        return {
          shop_id: item.shop_id,
          shop_name: item.shop_name,
          category_name: item.category_name,
          shop_img: item.shop_img
        }
      })
      // console.log(shoparr)
      return shoparr
    }
    return []

  },
  jumpDetailFn(event:any) {
    // console.log("事件对象:", event); // 打印整个事件对象以查看结构
    // console.log("当前目标数据集:", event.currentTarget.dataset); // 打印数据集内容
    // 从 event.currentTarget.dataset 中获取 shopId
    const shopId = event.currentTarget.dataset.shopid; // 使用 data-shop-id
    // console.log("点击的商店ID:", shopId); // 打印 shopId
  
    if (shopId) {
      wx.navigateTo({
        url: `../shopDetail/shopDetail?shop_id=${shopId}` // 跳转到目标页面
      });
    } else {
      console.error("未找到商店ID");
    }
  },
  
  methods: {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})